package com.isczy.acl.controller;


import com.isczy.acl.entity.Permission;
import com.isczy.acl.service.PermissionService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 */
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /***
     * ***************************************
     * 功能描述:获取全部菜单
     * @param
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:14】
     * @author modify:
     */
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public DataResult indexAllPermission() {
        try{
            List<Permission> list =  permissionService.queryAllMenuGuli();
            return DataResult.builder()
                    .data("children",list)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("查询所有菜单失败")
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:递归删除菜单
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:14】
     * @author modify:
     */
    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public DataResult remove(@PathVariable String id) {
        try{
            permissionService.removeChildByIdGuli(id);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("递归删除菜单失败")
                    .params(id)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:角色分配权限
     * @param roleId
     * @param permissionId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:15】
     * @author modify:
     */
    @ApiOperation(value = "角色分配权限")
    @PostMapping("/doAssign")
    public DataResult doAssign(String roleId,String[] permissionId) {
        try{
            permissionService.saveRolePermissionRealtionShipGuli(roleId,permissionId);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("角色分配权限失败")
                    .params(roleId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据角色获取菜单
     * @param roleId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:17】
     * @author modify:
     */
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public DataResult toAssign(@PathVariable String roleId) {
        try{
            List<Permission> list = permissionService.selectAllMenu(roleId);
            return DataResult.builder().data("children", list).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据角色获取菜单失败")
                    .params(roleId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:新增菜单
     * @param permission
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:18】
     * @author modify:
     */
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public DataResult save(@RequestBody Permission permission) {
        try{
            permissionService.save(permission);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据角色获取菜单失败")
                    .params(permission)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改菜单
     * @param permission
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:19】
     * @author modify:
     */
    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public DataResult updateById(@RequestBody Permission permission) {
        try{
            permissionService.updateById(permission);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("修改菜单失败")
                    .params(permission)
                    .build();
        }
    }

}

